import os
import sys
import time

from selenium.webdriver.common.by import By

from Base.base_action import BaseAction


class Login(BaseAction):
    # 按钮信息
    mine = (By.ID, "org.c2h4.afei.beauty:id/iv_mine")  # 我的
    login_now = (By.ID, "org.c2h4.afei.beauty:id/mine_tv_name")  # 我的界面，现在登录按钮
    wechat = (By.ID, "org.c2h4.afei.beauty:id/textView2")  # 微信登陆按钮
    QQ = (By.ID, "org.c2h4.afei.beauty:id/textView3")  # qq登录按钮
    we_blog = (By.ID, "org.c2h4.afei.beauty:id/textView4")  # 微博登陆按钮
    blog_confirm = (By.ID, "com.sina.weibo:id/new_bnLogin")
    mobile = (By.XPATH, "//*[contains(@text, '手机登录')]")  # 手机登录按钮
    mobile_confirm = (By.ID, "org.c2h4.afei.beauty:id/tv_login")  # 本机号码一键登录按钮
    change_number = (By.ID, "org.c2h4.afei.beauty:id/tv_phone_sms")  # 切换号码按钮
    enter_mobile_number = (By.ID, "org.c2h4.afei.beauty:id/et_num")  # 输入手机号码框
    get_verification_code = (By.ID, "org.c2h4.afei.beauty:id/tv_tip")  # 获取验证码按钮
    enter_verification_code = (By.ID, "org.c2h4.afei.beauty:id/et_vertify")  # 输入验证码框
    submit_button = (By.ID, "org.c2h4.afei.beauty:id/tv_submit")  # 提交按钮
    setting_btn = (By.ID, "org.c2h4.afei.beauty:id/main_ib_setting")  # 设置按钮
    logout_btn = (By.XPATH, "//*[contains(@text, '退出登录')]")  # 退出登录按钮
    logout_confirm = (By.XPATH, "//*[contains(@text,'确认')]")  # 退出时确认按钮
    back_btn = (By.CLASS_NAME, "android.view.View")  # 返回我的界面，按钮
    private_commit = (By.ID, "org.c2h4.afei.beauty:id/tv_private_commit")  # 首次下载时app权限申请
    auth_allowed = (By.XPATH, "//*[contains(text(), '允许')]")  # 手机弹窗允许按钮
    guide_page = (By.ID, "org.c2h4.afei.beauty:id/image")  # 引导页
    casual_see = (By.XPATH, "//*[contains(@text, '先随便看看')]")  # 手机弹窗允许按钮
    swipe_direction = (500, 1000, 200, 1000, 500)
    message_text = (By.XPATH, "//*[contains(@text,'验证码')]")  # 获取短信验证码的定位

    # 点击允许权限
    def private_allowed(self):
        try:
            self.click_element(self.private_commit)
            els = self.driver.find_elements_by_class_name('android.widget.Button')
            for el in els:
                if el.text == '允许':
                    self.driver.find_element_by_android_uiautomator('new UiSelector().text("允许")').click()
                    break
                elif el.text == '始终允许':
                    self.driver.find_element_by_android_uiautomator('new UiSelector().text("始终允许")').click()
                    break
                elif el.text == '本次运行允许':
                    self.driver.find_element_by_android_uiautomator('new UiSelector().text("本次运行允许")').click()
                    break
                elif el.text == '仅在使用中允许':
                    self.driver.find_element_by_android_uiautomator('new UiSelector().text("仅在使用中允许")').click()
                    break
                elif el.text == '确定':
                    self.driver.find_element_by_android_uiautomator('new UiSelector().text("确定")').click()
                    break

            time.sleep(1)

            # 引导页滑动
            for i in range(4):
                self.swipe(*self.swipe_direction)
            self.click_element(self.casual_see)  # 点击先随便看看
        except:
            return

    # 判断是否登录，如果登陆就退出登录
    def judge_if_login(self):
        self.private_allowed()  # 允许权限
        self.click_element(self.mine)
        s = self.find_element(self.login_now).text
        # 如果已经登陆就退出登录
        if s != "现在登录":
            self.click_element(self.setting_btn)
            self.click_element(self.logout_btn)
            self.click_element(self.logout_confirm)
            self.click_element(self.back_btn)
        return

    # 通过本机手机登录
    def mobile_login(self):
        self.judge_if_login()
        self.click_element(self.mine)  # 点击我的
        self.click_element(self.login_now)  # 点击现在登陆
        self.click_element(self.mobile)  # 点击手机登录
        self.click_element(self.mobile_confirm)  # 点击本机号码一键登录
        file_name = sys._getframe().f_code.co_name  # 获取函数名作为截图名称
        time.sleep(15)  # 休眠五秒（可以延长），防止网络或手机卡顿等原因
        self.screen_shot(r"D:\app_auto_test\ScreenShot" + os.sep + file_name + ".png")  # 登录后截图

    # 切换号码登陆
    def change_number_login(self, phone_number):
        self.judge_if_login()
        self.click_element(self.mine)  # 点击我的
        self.click_element(self.login_now)  # 点击现在登陆
        self.click_element(self.mobile)  # 点击手机登录
        self.click_element(self.change_number)  # 点击切换号码按钮
        self.input_keys(self.enter_mobile_number, phone_number)  # 输入手机号
        self.click_element(self.get_verification_code)  # 点击获取验证码
        time.sleep(15)  # 确保能收到短信，休眠15s，根据实际情况可以适当延长或缩短
        self.pull_notification()  # 下拉通知栏
        text = self.find_element(self.message_text).text[-4:]
        self.key_event(4)
        while True:
            try:
                self.input_keys(self.enter_verification_code, text)
                time.sleep(2)
                self.click_element(self.submit_button)
                break
            except:
                pass
        file_name = sys._getframe().f_code.co_name  # 获取函数名作为截图名称
        time.sleep(5)
        self.screen_shot(r"D:\app_auto_test\ScreenShot" + os.sep + file_name + ".png")  # 登录后截图

    # 通过微信登陆
    def wechat_login(self):
        self.judge_if_login()
        self.click_element(self.mine)  # 点击我的
        self.click_element(self.login_now)  # 点击现在登陆
        self.click_element(self.wechat)  # 点击微信登陆
        file_name = sys._getframe().f_code.co_name  # 获取函数名作为截图名称
        time.sleep(5)  # 休眠五秒（可以延长），防止网络或手机卡顿等原因
        self.screen_shot(r"D:\app_auto_test\ScreenShot" + os.sep + file_name + ".png")  # 登录后截图

    # 通过QQ登录
    def qq_login(self):

        self.judge_if_login()
        self.click_element(self.mine)  # 点击我的
        self.click_element(self.login_now)  # 点击现在登陆
        self.click_element(self.QQ)  # 点击QQ登陆
        file_name = sys._getframe().f_code.co_name  # 获取函数名作为截图名称
        time.sleep(5)  # 休眠五秒（可以延长），防止网络或手机卡顿等原因
        self.screen_shot(r"D:\app_auto_test\ScreenShot" + os.sep + file_name + ".png")  # 登录后截图

    # 通过微博登陆
    def we_blog_login(self):
        self.judge_if_login()
        self.judge_if_login()
        self.click_element(self.mine)  # 点击我的
        self.click_element(self.login_now)  # 点击现在登陆
        self.click_element(self.we_blog)  # 点击微博登陆
        file_name = sys._getframe().f_code.co_name  # 获取函数名作为截图名称
        stamp = str(time.time()).split(".")[0]
        time.sleep(5)  # 休眠五秒（可以延长），防止网络或手机卡顿等原因
        self.screen_shot(r"D:\app_auto_test\ScreenShot" + os.sep + file_name + stamp + ".png")  # 登录后截图
